package com.example.peibei.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.widget.TextView;

import com.dingtao.common.bean.User;
import com.dingtao.common.core.DataCall;
import com.dingtao.common.core.WDActivity;
import com.dingtao.common.core.exception.ApiException;
import com.dingtao.common.util.SPUtils;
import com.dingtao.common.util.UIUtils;
import com.example.peibei.R;
import com.example.peibei.base.SpConstant;
import com.example.peibei.service.presenter.ChangePswPresenter;
import com.example.peibei.service.presenter.SetPswPresenter;
import com.example.peibei.ui.weight.CEditText;

import butterknife.BindView;
import butterknife.OnClick;
import cn.jpush.android.cache.Sp;

public class SettingPayPswActivity extends WDActivity {
    @BindView(R.id.cedittext)
    CEditText cedittext;
    @BindView(R.id.tv_set_title)
    TextView tv_set_title;

    private SPUtils spUtils;
    private String password;
    private String token;
    private boolean isFirst = true;
    private SetPswPresenter setPswPresenter;
    private ChangePswPresenter changePswPresenter;
    private String paypsw = "";

    @Override
    protected int getLayoutId() {
        return R.layout.activity_set_pay_psw;
    }

    @Override
    protected void initView() {
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        if (bundle!=null){
            paypsw = bundle.getString("paypsw");
        }
        if (TextUtils.isEmpty(paypsw)) {
            isFirst = true;
        } else {
            isFirst = false;
        }
        spUtils = new SPUtils(SettingPayPswActivity.this, SpConstant.USER_TOKEN);
        password = spUtils.getString(SpConstant.PAY_PWD);
        token = spUtils.getString(SpConstant.USER_TOKEN);
        if (password.equals("false")) {
            tv_set_title.setText(isFirst ? "设置支付密码" : "请输入支付密码");
        } else {
            tv_set_title.setText(isFirst ? "请输入原支付密码" : "请输入新支付密码");
        }
        setPswPresenter = new SetPswPresenter(new SetPswCall());
        changePswPresenter = new ChangePswPresenter(new ChangePswCall());
        cedittext.setOnFinishListener(new CEditText.OnFinishListener() {
            @Override
            public void onFinish(String msg) {
                if (isFirst) {
                    Bundle bundle = new Bundle();
                    bundle.putString("paypsw", msg);
                    intent(SettingPayPswActivity.class,bundle);
                    finish();
                } else {
                    if (password.equals("false")) {
                        if (paypsw.equals(msg)) {
                            //设置支付密码
                            setPswPresenter.reqeust("Bearer " + token, msg, "");
                        } else {
                            UIUtils.showToastSafe("两次输入的密码不一致");
                        }
                    } else {
                        //修改支付密码
                        changePswPresenter.reqeust("Bearer " + token, paypsw, msg);
                    }
                }
            }
        });
    }

    @OnClick(R.id.iv_back)
    public void back() {
        finish();
    }

    class SetPswCall implements DataCall<String> {

        @Override
        public void success(String data, Object... args) {
            spUtils.putString(SpConstant.PAY_PWD,"true");
            UIUtils.showToastSafe("设置密码成功");
            User loginUser = SpConstant.getLoginUser();
            loginUser.setPayPwd("true");
            SpConstant.login(loginUser);
            finish();
        }

        @Override
        public void fail(ApiException e, Object... args) {
            UIUtils.showToastSafe("设置失败：" + e.getDisplayMessage());
        }
    }

    class ChangePswCall implements DataCall<String> {

        @Override
        public void success(String data, Object... args) {
            spUtils.putString(SpConstant.PAY_PWD,"true");
            UIUtils.showToastSafe("修改密码成功");
            finish();
        }

        @Override
        public void fail(ApiException e, Object... args) {
            UIUtils.showToastSafe("修改失败：" + e.getDisplayMessage());
        }
    }

    @Override
    protected void destoryData() {

    }
}
